package cn.mesmile.activity.listener1;

import cn.hutool.core.util.ObjectUtil;
import org.activiti.bpmn.model.UserTask;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.stereotype.Component;

/**
 * @author zb
 * @date 2022/1/11 21:35
 * @Description 判断是否自动跳过任务
 */
@Component
public class SkipTaskListener implements ExecutionListener {

    /**
     * 序列化
     */
    private static final long serialVersionUID = 1L;

    @Override
    public void notify(DelegateExecution execution) {
        Object skip = execution.getVariable("skip");
        if (ObjectUtil.isNotEmpty(skip) && BooleanUtils.toBoolean(skip.toString())){
            //通过设置流程变量_FLOWABLE_SKIP_EXPRESSION_ENABLED为true启动skipExpression属性,
            //必须是true而非字符串"true",若不启动是不生效的
            // _FLOWABLE_SKIP_EXPRESSION_ENABLED
            // _ACTIVITI_SKIP_EXPRESSION_ENABLED
            execution.setVariable("_ACTIVITI_SKIP_EXPRESSION_ENABLED",true);
            UserTask userTask=(UserTask)execution.getCurrentFlowElement();
//            userTask.setCandidateUsers(Arrays.asList("zhangsan","lisi"));
            // 设置自动跳过
            userTask.setSkipExpression("${1==1}");
        }

    }
}
